import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import { Checkbox as AntdCheckbox } from 'antd';
import React, { useEffect } from 'react';
import createField from '../../../createField';
import useOptions from '../../../hooks/useOptions';
var _excluded = ['children', 'options'];
var Checkbox = function Checkbox(_ref) {
  var children = _ref.children,
    optionsProp = _ref.options,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useOptions = useOptions(optionsProp),
    options = _useOptions.options;
  useEffect(
    function () {
      var existed =
        (rest === null || rest === void 0 ? void 0 : rest.value) && options.length
          ? options.some(function (option) {
              var _rest$value;
              return (_rest$value = rest.value) === null || _rest$value === void 0
                ? void 0
                : _rest$value.includes(option.value);
            })
          : false;
      if ((rest === null || rest === void 0 ? void 0 : rest.value) && !existed) {
        var _rest$onChange;
        (_rest$onChange = rest.onChange) === null || _rest$onChange === void 0
          ? void 0
          : _rest$onChange.call(rest, []);
      }
    },
    [options],
  );
  return /*#__PURE__*/ React.createElement(
    AntdCheckbox.Group,
    _objectSpread(
      _objectSpread({}, rest),
      {},
      {
        options: options,
      },
    ),
    children,
  );
};
export default createField(Checkbox);
